<script setup>
  import { ref, computed } from 'vue'
  import { useRouter } from 'vue-router'
  import { storeToRefs } from 'pinia'
  import useCityStore from '@/stores/modules/city'
  import useHomeStore from '@/stores/modules/home'
  import { formatMonthDay, getDiffDays } from '@/utils/format_date'
  import useMainStore from '@/stores/modules/main'

  // 获取路由对象
  const router = useRouter()

  // 选择城市
  const cityClick = () => {
    router.push('/city')
  }

  // 当前城市
  const cityStore = useCityStore()
  const { currentCity } = storeToRefs(cityStore)

  // 获取当前设备的地理位置信息
  const positionClick = () => {
    navigator.geolocation.getCurrentPosition((res) => {
      console.log('获取位置信息成功：', res)
    }, (err) => {
      console.log('获取位置信息失败：', err)
    })
  }


  // 日期范围处理
  // getDate(): 获取日期的天数
  // setDate(): 设置日期的天数
  const mainStore = useMainStore()
  const { startDate, endDate } = storeToRefs(mainStore)

  const startDateStr = computed(() => formatMonthDay(startDate.value))
  const endDateStr = computed(() => formatMonthDay(endDate.value))
  const stayCount = ref(1)

  // 选择入住时间范围
  // 控制日期选择组件的显示/隐藏 
  const showCalendar = ref(false);
  // 选好时间，点击确认后执行的回调函数
  const onConfirm = (values) => {
    // values[0]：选择的开始时间
    // values[1]：选择的结束时间
    
    // 设置日期
    const selectStartDate = values[0] 
    const selectEndDate = values[1] 

    // 设置开始日期和结束日期
    mainStore.startDate = selectStartDate
    mainStore.endDate = selectEndDate

    // 计算居住天数
    // 方法 1
    // const totalMilliseconds = +end - +start
    // stayCount.value = milliseconds / ( 24 * 60 * 60 * 1000) + 1
    // 方法 2
    stayCount.value = getDiffDays(selectStartDate, selectEndDate)

    // 隐藏日期选择器
    showCalendar.value = false;
  };

  // 格式化日期
  const formatter = (day) => {
    const month = day.date.getMonth() + 1;
    const date = day.date.getDate();

    // 当前日期
    const nowMonth = new Date().getMonth() + 1
    const nowDay = new Date().getDate()

    if (month === nowMonth && nowDay === date) {
      day.text = '今天';
    }

    if (month === 5) {
      if (date === 1) {
        day.topInfo = '劳动节';
      } else if (date === 4) {
        day.topInfo = '青年节';
      }
    }

    if (day.type === 'start') {
      day.bottomInfo = '入住';
    } else if (day.type === 'end') {
      day.bottomInfo = '离店';
    }

    return day;
  };

  // 热门建议
  const homeStore = useHomeStore()
  const { hotSuggests } = storeToRefs(homeStore)

  const searchBtnClick = () => {
    router.push({
      path: '/search',
      query: {
        startDate: startDate.value,
        endDate: endDate.value,
        currentCity: currentCity.value.cityName
      }
    })
  }
</script>

<template>
  <div class="search-box">
    <!-- 位置信息 -->
    <div class="location bottom-gray-line">
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>
      <div class="position" @click="positionClick">
        <span class="text">我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="">
      </div>
    </div>

    <!-- 日期范围 -->
    <div class="section date-range bottom-gray-line" @click="showCalendar = true">
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time">{{ startDateStr }}</span>
        </div>
        <div class="stay">共 {{ stayCount }} 晚</div>
      </div>
      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time">{{ endDateStr }}</span>
        </div>
      </div>
    </div>
    <!-- 日期选择器 -->
    <van-calendar title="请选择入住时间范围" v-model:show="showCalendar" 
      :show-confirm="false" type="range" :round="false"
      color="#ff9854" :formatter="formatter" @confirm="onConfirm" />

    <!-- 价格/人数选择 -->
    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>

    <!-- 关键字 -->
    <div class="section keyword bottom-gray-line">关键字/位置/民宿名</div>

    <!-- 热门建议 -->
    <div class="section hot-suggest">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div 
          class="item"
          :style="{ color: item.tagText.color, backgroundColor: item.tagText.background.color }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>

    <!-- 搜索按钮 -->
    <div class="section search-btn">
      <div class="btn" @click="searchBtnClick">开始搜索</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.search-box {
  // 日期选择组件的高度
  --van-calendar-popup-height: 100%;
}

// 位置信息
.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;

  .city {
    flex: 1;
    font-size: 15px;
  }

  .position {
    width: 74px;

    .text {
      position: relative;
      font-size: 12px;
      top: 0.5px;
    }

    img {
      width: 18px;
      height: 18px;
      margin-left: 5px;
    }
  }

}

.section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;
  height: 44px;

  .start {
    flex: 1;
    display: flex;
    height: 44px;
    align-items: center;
  }

  .end {
    min-width: 30%;
    padding-left: 20px;
  }

  .date {
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
      color: #999;
    }

    .time {
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
}

// 日期范围
.date-range {
  height: 44px;

  .stay {
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666;
  }
}

.price-counter {
  .start {
    border-right: 1px solid var(--line-color);
  }
}

.hot-suggest {
  margin: 10px 0;
  height: auto;
  .item {
    padding: 4px 8px;
    margin: 5px 3px;
    border-radius: 14px;
    font-size: 12px;
    line-height: 1;
  }
}

.search-btn {
  .btn {
    width: 342px;
    height: 38px;
    max-height: 50px;
    font-weight: 500;
    font-size: 18px;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    color: #fff;
    background-image: var(--theme-linear-gradient);
  }
}

</style>